/*
 * Powered By [pkuWx]
 * Web Site: http://www.wuxi.ss.pku.edu.cn
 * Since 2015 - 2015
 */


package cn.edu.pku.wxlib.book.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.pku.wxlib.book.model.Book;
import cn.edu.pku.wxlib.book.service.BookManager;
import cn.edu.pku.wxlib.book.vo.query.BookQuery;
import cn.edu.pku.wxlib.common.authority.Login;
import cn.org.rapid_framework.web.scope.Flash;
import javacommon.base.BaseRestSpringController;
import javacommon.page.Page;

/**
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("/book")
public class BookController extends BaseRestSpringController<Book,java.lang.Integer>{
		
	private final String LIST_ACTION = "redirect:/book";
	
	private BookManager bookManager;
	public void setBookManager(BookManager bookManager) {
		this.bookManager = bookManager;
	}
	
	@InitBinder  
	public void initBinder(WebDataBinder binder) {  
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));  
	}
	   
	/** 存放共享变量,或初始化操作 */
	@ModelAttribute
	public void init(ModelMap model) {}
	
	/** 列表 */
	@Login @RequestMapping
	public String index(ModelMap model,BookQuery query,HttpServletRequest request,HttpServletResponse response) {
		Page page = this.bookManager.findPage(query);
		model.addAllAttributes(toModelMap(page, query));
		return "/book/index";
	}
	
	/** 显示 */
	@Login @RequestMapping(value="/{id}")
	public String show(ModelMap model,@PathVariable java.lang.Integer id) throws Exception {
		Book book = (Book)bookManager.getById(id);
		model.addAttribute("book",book);
		return "/book/show";
	}

	/** 进入新增 */
	@Login @RequestMapping(value="/new")
	public String _new(ModelMap model,Book book,HttpServletRequest request,HttpServletResponse response) throws Exception {
		model.addAttribute("book",book);
		return "/book/new";
	}
	
	/** 保存新增 */
	@Login @RequestMapping(method=RequestMethod.POST)
	public String create(ModelMap model,@Valid Book book,BindingResult errors,HttpServletRequest request,HttpServletResponse response) throws Exception {
		bookManager.save(book);
		Flash.current().success(CREATED_SUCCESS); 
		return LIST_ACTION;
	}
	
	/** 编辑 */
	@Login @RequestMapping(value="/{id}/edit")
	public String edit(ModelMap model,@PathVariable java.lang.Integer id) throws Exception {
		Book book = (Book)bookManager.getById(id);
		model.addAttribute("book",book);
		return "/book/edit";
	}
	
	/** 保存更新 */
	@Login @RequestMapping(value="/{id}",method=RequestMethod.PUT)
	public String update(ModelMap model,@PathVariable java.lang.Integer id,@Valid Book book,BindingResult errors,HttpServletRequest request,HttpServletResponse response) throws Exception {
		bookManager.update(book);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}
	
	/** 删除 */
	@Login @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	public String delete(ModelMap model,@PathVariable java.lang.Integer id) {
		bookManager.removeById(id);
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}

	/** 批量删除 */
	@Login @RequestMapping(method=RequestMethod.DELETE)
	public String batchDelete(ModelMap model,@RequestParam("items") java.lang.Integer[] items) {
		for(int i = 0; i < items.length; i++) {
			bookManager.removeById(items[i]);
		}
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}	

	/**查询 */
	@Login @RequestMapping(value="/search")
	public String FindByName(ModelMap model,BookQuery query) throws Exception {
		System.out.println(query.getBookName());
//		Book book = (Book)bookManager.getById(id);
		Page page = this.bookManager.findPage(query);
		model.addAttribute("page",page);
		return "/book/index";
	}

}

